package com.htht.smartschool.util.system;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
   
public class ImgUtil {   
	private ImgUtil() {}  
	static public String usersimgpath = "";
	static public String studentsimgpath = "";
	private static BASE64Encoder encoder = new BASE64Encoder();    
	private static BASE64Decoder decoder = new BASE64Decoder();    
	static public String getImageBinary(String filepath){    
        File f = new File(filepath);           
        BufferedImage bi;    
        try {    
            bi = ImageIO.read(f);    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();    
            ImageIO.write(bi, "png", baos);    
            byte[] bytes = baos.toByteArray();    
            System.out.println(encoder.encodeBuffer(bytes).trim());
            return encoder.encodeBuffer(bytes).trim();    
        } catch (IOException e) {    
            e.printStackTrace();    
        }    
        return null;    
    }    
        
	static public void base64StringToImage(String base64String,String filepath,File file){    
        try {    
            byte[] bytes1 = decoder.decodeBuffer(base64String);    
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);    
            BufferedImage bi1 =ImageIO.read(bais);    
            File w2 = new File(filepath);
            if(!file.exists()){
            	w2.mkdirs();
            }
            ImageIO.write(bi1, "png", w2);
        } catch (IOException e) {    
            e.printStackTrace();    
        }    
    }    
   public static void remove(String fileurl,String imgurl) {
	   String[] urls = imgurl.split("/");
	   String url = urls[urls.length-1];
	   String filepath = fileurl+"/"+url;
	   File file = new File(filepath);
	   if(file.exists()){
		   file.delete();
	   }
   }
} 